package com.huewu.pla.pinterest.internal;

import opens.components.view.RemoteImageView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.idreems.zakerlike.MyApplication;
import com.idreems.zakerlike.R;
import com.lurencun.android.adapter.ViewBuilder;

/**
 * @author : 桥下一粒砂
 * @email : chenyoca@gmail.com
 * @date : 2012-12-6
 * @desc : TODO
 */
public class SimpleViewBuilder extends ViewBuilder<ImageWrapper> {

    @Override
    public View createView(LayoutInflater inflater, int position, ImageWrapper data) {
        View view = inflater.inflate(R.layout.oneimage_onetext, null);
        updateView(view, position, data);
        return view;
    }

    @Override
    public void updateView(View view, int position, ImageWrapper data) {
        //n kind of colors
        final int kColorKinds = 6;
        int kColors[]={0x98F5FF,0x90EE90,0x87CEFF,0xFFBBFF,0xFFDEAD,0xDDA0DD};
        if(kColorKinds>kColors.length)
        {
        	throw new IndexOutOfBoundsException();
        }
        long index = data.id%kColorKinds;
        
        view.setBackgroundColor(0xFF000000+kColors[(int)index]);
        
        RemoteImageView image = (RemoteImageView) view.findViewById(R.id.thumbnail);
        TextView text = (TextView) view.findViewById(R.id.text);
        text.setText(data.description);
        

        if (!TextUtils.isEmpty(data.url)) {
            LinearLayout.LayoutParams params = (LayoutParams) image.getLayoutParams();
            params.width = data.width;
            image.setLayoutParams(params);
            image.setAdjustViewBounds(false);
            image.setImageResource(R.drawable.content_loading);
            image.loadFromUrl(data.url, MyApplication.getCache());
            image.invalidate();
            image.setVisibility(View.VISIBLE);
        }
        else {
            image.setVisibility(View.GONE);
        }
    }
}
